//
//  STLAvPlayer.m
//  STL_ Animation
//
//  Created by DJtd on 16/4/5.
//  Copyright © 2016年 STL. All rights reserved.
//

#import "STLAvPlayer.h"

@implementation STLAvPlayer

+(instancetype)shareAVPlayer {
    
    static STLAvPlayer *player = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        player = [[STLAvPlayer alloc] init];
        player.isPlayer = YES;
        player.isCollection = NO;
        player.isLock = NO;
        player.isRadom = NO;
        player.isDownload = NO;
        player.changeItem = 0;
        player.changeType = 0;
        player.indexPathNum = 0;
        player.MVList = [NSMutableArray arrayWithCapacity:0];
        player.progressView = [[UIProgressView alloc] init];
        player.progressView.tintColor = [UIColor grayColor];
        player.progressView.progressViewStyle = UIProgressViewStyleDefault;
    });
    
    return player;
}
- (void)playWithUrl:(NSString *)url {
    
    if (self.currentItem) {
//        [self removeObserverFromPlayerItem:self.currentItem];

//        [self.progressView removeFromSuperview];
        
        [self.playerItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
    }
    
    self.playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:url]];
    
    [self.playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
    
    
    [self replaceCurrentItemWithPlayerItem:self.playerItem];

    //////avplaver播放结束系统自动发送通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(replaceButtonHiddenYES) name:AVPlayerItemDidPlayToEndTimeNotification object:self.currentItem];
}





- (void)replaceButtonHiddenYES{

    NSLog(@"当前视频播放完毕");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"yes" object:nil];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


//
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    
    
    if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
        if (self.currentItem.duration.value) {

            NSTimeInterval timeInterval = [self availableDuration];// 计算缓冲进度
//            NSLog(@"Time Interval:%f",timeInterval);
            CMTime duration = self.playerItem.duration;
            CGFloat totalDuration = CMTimeGetSeconds(duration);
            
            
            if ([[NSString stringWithFormat:@"%f", totalDuration] isEqualToString:@"nan"]) {
                totalDuration = 1;
            }
            if ([[NSString stringWithFormat:@"%f", timeInterval] isEqualToString:@"nan"]) {
                timeInterval = 0;
            }
            
            [self.progressView setProgress:timeInterval / totalDuration animated:YES];
        }
    }
}
- (NSTimeInterval)availableDuration {
    
    NSArray *loadedTimeRanges = [[self currentItem] loadedTimeRanges];
    CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];// 获取缓冲区域
    float startSeconds = CMTimeGetSeconds(timeRange.start);
    float durationSeconds = CMTimeGetSeconds(timeRange.duration);
    NSTimeInterval result = startSeconds + durationSeconds;// 计算缓冲总进度
    return result;
    
}





@end



